package com.xyy.saas.payment.payment.core.service.reconciliation;

import com.xyy.saas.payment.common.enums.ReconciliationType;
import com.xyy.saas.payment.payment.core.dto.ReconcileContext;
import com.xyy.saas.payment.trace.AlarmService;
import com.xyy.saas.payment.trace.DingTalkMessage;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author wangzeqi
 */
@Slf4j
@Component
public class ReconciliationExecutor {

    @Resource
    private ReconciliationStrategyFactory strategyFactory;
    @Resource
    private AlarmService alarmService;
    @Value("${reconcile.webChatWebHook:''}")
    private String webChatWebHook;
    @Value("${reconcile.webChatMobiles:''}")
    private String webChatMobiles;
    /*@Resource(name = "reconcileThreadPool")
    private ThreadPoolExecutor reconcileThreadPool;*/

    public void execute(List<ReconciliationType> types, ReconcileContext context) {

        if (CollectionUtils.isEmpty(types)) {
            log.error("reconciliationExecutor reconciliationTypes is empty");
            return;
        }

        for (ReconciliationType type : types) {
            try {
                ReconciliationStrategy strategy = strategyFactory.getStrategy(type);
                if (strategy != null) {
                    strategy.execute(context);
                } else {
                    log.warn("reconciliationExecutor unknown reconciliationStrategy: {}", type);
                }
            } catch (Exception e) {
                log.error("[对账] 策略执行失败 | 类型: {} | 错误: {}", type, e.getMessage(), e);
                alarmService.alarm(webChatWebHook, webChatMobiles,
                        DingTalkMessage.builder()
                                .type("对账策略执行失败")
                                .method("ReconciliationExecutor#execute")
                                .msg("错误信息：" + e.getMessage())
                                .build());
            }
        }

        /*CompletableFuture.allOf(types.stream().map(type -> CompletableFuture.runAsync(
                        () -> {
                            try {
                                ReconciliationStrategy strategy = strategyFactory.getStrategy(type);
                                if (strategy != null) {
                                    strategy.execute(context);
                                } else {
                                    log.warn("reconciliationExecutor unknown reconciliationStrategy: {}", type);
                                }
                            } catch (Exception e) {
                                log.error("[对账] 策略执行失败 | 类型: {} | 错误: {}", type, e.getMessage(), e);
                                // alarm
                            }
                        },
                        executor))
                .toArray(CompletableFuture[]::new)).join();*/
    }

}
